package com.hexu.event.dao;

import com.hexu.event.entity.Specie;
import com.hexu.event.entity.Test;
import com.hexu.event.util.JdbcUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;


public class TestDao {
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;

    //添加名称和图片
    public boolean addTest(Test test) throws Exception{
        boolean flag = false;
        String sql="insert into uploading(name,imgurl) values(?,?)";
        conn = JdbcUtil.getConn();
        ps = conn.prepareStatement(sql);
        ps.setString(1,test.getName());
        ps.setString(2,test.getImgurl());
        if(ps.executeUpdate()>0){
            flag=true;
        }
        JdbcUtil.close(conn, ps, null);
        return flag;
    }
    //遍历
    public List<Test> findAll() throws Exception{
        List<Test> list =new ArrayList<Test>();
        String sql="select * from uploading";
        conn = JdbcUtil.getConn();
        ps = conn.prepareStatement(sql);
        rs = ps.executeQuery();
        while(rs.next()){
            Test test = new Test();
            test.setId(rs.getInt(1));
            test.setName(rs.getString(2));
            test.setImgurl(rs.getString(3));
            list.add(test);
        }
        JdbcUtil.close(conn, ps, rs);
        return list;

    }

}
